from .settings_base import *


PROJECT = 'structure_monitor'
EXEC_ENV = 'dev'DEBUG = True


class MysqlConfig:
    """mysql环境"""

    @classmethod
    def env(cls):
        """执行环境"""
        if EXEC_ENV == 'dev':
            return cls.Dev
        if EXEC_ENV == 'test':
            return cls.Test
        if EXEC_ENV == 'prod':
            return cls.Prod

    class Dev:
        """开发环境"""
        host = '192.168.18.13'
        port = '3306'
        db = 'db_{}'.format(PROJECT)
        user = 'root'
        pwd = 'root'

    class Test:
        """测试环境"""
        host = '114.116.69.21'
        port = '3306'
        db = 'db_{}_test'.format(PROJECT)
        user = '{}_test'.format(PROJECT)
        pwd = 'MySQL1234!'

    class Prod:
        """生产环境"""
        host = '114.116.69.21'
        port = '3306'
        db = 'db_{}'.format(PROJECT)
        user = PROJECT
        pwd = 'MySQL1234!'


class RedisConfig:
    """redis环境"""

    @classmethod
    def env(cls):
        """执行环境"""
        if EXEC_ENV == 'dev':
            return cls.Dev
        if EXEC_ENV == 'test':
            return cls.Test
        if EXEC_ENV == 'prod':
            return cls.Prod

    @classmethod
    def get_env(cls, env):
        """执行环境"""
        if env == 'dev':
            return cls.Dev
        if env == 'test':
            return cls.Test
        if env == 'prod':
            return cls.Prod

    class Dev:
        """开发环境"""
        host = '192.168.18.13'
        port = 6377
        db = '12'
        pwd = 'Redis1234!'

    class Test:
        """测试环境"""
        host = '114.116.13.1'
        port = 6377
        db = '12'
        pwd = 'Redis1234!'

    class Prod:
        """生产环境"""
        host = '114.115.208.167'
        port = 6377
        db = '12'
        pwd = 'Redis1234!'


class InfluxConfig:
    """influxdb环境"""

    @classmethod
    def env(cls):
        """执行环境"""
        if EXEC_ENV == 'dev':
            return cls.Dev
        if EXEC_ENV == 'test':
            return cls.Test
        if EXEC_ENV == 'prod':
            return cls.Prod

    class Dev:
        """开发环境"""
        host = '192.168.18.13'
        port = 8086
        db = 'db_{}'.format(PROJECT)
        user = 'admin'
        pwd = 'cloudansys[admin]'
        sh_user = 'root'
        sh_pwd = 'root'

    class Test:
        """测试环境"""
        host = '114.115.142.218'
        port = 8086
        db = 'db_{}_test'.format(PROJECT)
        user = 'admin'
        pwd = 'cloudansys[admin]'
        sh_user = 'root'
        sh_pwd = 'Un?Y@m7%9!4LH'

    class Prod:
        """生产环境"""
        host = '114.115.142.218'
        port = 8086
        db = 'db_{}'.format(PROJECT)
        user = 'admin'
        pwd = 'cloudansys[admin]'
        sh_user = 'root'
        sh_pwd = 'Un?Y@m7%9!4LH'


redis_config = RedisConfig.env()

# channel layers backends
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [
                "redis://:{}@{}:{}/{}".format(redis_config.pwd, redis_config.host, redis_config.port, redis_config.db)
            ],
        },
    }
}

# Redis Cache
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://{}:{}/{}".format(redis_config.host, redis_config.port, redis_config.db),
        "TIMEOUT": 60 * 60 * 24,
        "OPTIONS": {
            # 自动将byte转成字符串
            "DECODE_RESPONSES": True,
            "PASSWORD": redis_config.pwd,
            "CONNECTION_POOL_KWARGS": {"max_connections": 100},
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "COMPRESSOR": "django_redis.compressors.lzma.LzmaCompressor",
        },
        "KEY_PREFIX": PROJECT + ":django",
    },
}

# 运行环境配置
msql_config = MysqlConfig.env()

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': msql_config.db,
        'USER': msql_config.user,
        'PASSWORD': msql_config.pwd,
        'HOST': msql_config.host,
        'PORT': msql_config.port,
        'OPTIONS': {
            "init_command": "SET foreign_key_checks = 0;",
        }
    }
}


def get_log_name():
    return os.path.split(__file__)[-1].split(".")[0].split("_")[1]


# 日志配置
LOGS_NAME = '{}.log'.format(get_log_name())
LOGGING = {
    # 指明dictConnfig的版本，目前就只有一个版本
    'version': 1,
    # 是否禁止默认配置的记录器
    'disable_existing_loggers': False,
    # 格式器
    'formatters': {
        # 详细
        'standard': {
            'format': '%(asctime)s %(levelname)s %(module)s.%(funcName)s:%(lineno)d %(message)s',
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
        # 简单
        'simple': {
            'format': '%(asctime)s %(levelname)s %(message)s',
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
    },
    # 处理器
    'handlers': {
        # 流处理器(控制台)
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'standard',
        },
        # 文件处理器
        'file': {
            'level': 'INFO',
            # 时间滚动切分
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': os.path.join(LOGS_DIR, LOGS_NAME),
            'formatter': 'standard',
            # 每天凌晨切分
            'when': 'MIDNIGHT',
            # 保存 7 天
            'backupCount': 7,
            'encoding': 'utf-8',
        },
    },
    # 记录器
    'loggers': {
        'django': {
            'handlers': ['console', 'file'],
            'level': 'INFO',
            'propagate': True
        },
    }
}

# 自定义图标
SIMPLEUI_CONFIG = {
    'system_keep': False,
    'dynamic': True,
    'menu_display': ['基础信息', '用户权限', '数据操作', '可视化', '数据模拟', '操作记录'],
    'menus': [
        {
            'name': '基础信息',
            'icon': 'fas fa-cogs',
            'models': [
                {
                    'name': '项目信息',
                    'icon': 'fas fa-flag',
                    'url': '/admin/pms/baseproject/'
                },
                {
                    'name': '类型信息',
                    'icon': 'fas fa-tags',
                    'url': '/admin/pms/basetargettype/'
                },
                {
                    'name': '指标信息',
                    'icon': 'fas fa-tag',
                    'url': '/admin/pms/settingsdataquota/'
                },
                {
                    'name': '分区信息',
                    'icon': 'fab fa-gofore',
                    'url': '/admin/pms/basetargetsitegroup/'
                },
                {
                    'name': '位置信息',
                    'icon': 'fas fa-map-marker-alt',
                    'url': '/admin/pms/basetargetsite/'
                },
                {
                    'name': '测点信息',
                    'icon': 'fas fa-crosshairs',
                    'url': '/admin/pms/basetarget/'
                },
                {
                    'name': '预警信息',
                    'icon': 'fas fa-exclamation-circle',
                    'url': '/admin/pms/warnbase/'
                },
                {
                    'name': '阈值信息',
                    'icon': 'fas fa-ruler-vertical',
                    'url': '/admin/pms/settingsthreshold/'
                },
            ]
        },
        {
            'name': '用户权限',
            'icon': 'fas fa-shield-alt',
            'models': [
                {
                    'name': '用户管理',
                    'icon': 'fas fa-user-cog',
                    'url': '/admin/auth/user/'
                },
                {
                    'name': '分组管理',
                    'icon': 'fas fa-users',
                    'url': '/admin/auth/group/'
                },
            ]
        },
        {
            'name': '数据操作',
            'icon': 'fas fa-database',
            'models': [
                {
                    'name': '删除记录',
                    'icon': 'fas fa-trash-alt',
                    'url': '/admin/pms/pydbhandle/'
                },
                {
                    'name': '删除条件',
                    'icon': 'fas fa-trash-restore',
                    'url': '/admin/pms/pydbcondition/'
                },
            ]
        },
        {
            'name': '可视化',
            'icon': 'fa fa-eye',
            'models': [
                {
                    'name': 'MapGlobe',
                    'icon': 'fas fa-globe-asia',
                    'url': '/chart/globe/'
                },
                {
                    'name': 'Surface3D',
                    'icon': 'fab fa-ethereum',
                    'url': '/chart/surface/'
                },
                {
                    'name': 'ParticleWhirl',
                    'icon': 'fas fa-magic',
                    'url': '/chart/whirl/'
                },
            ]
        },
        {
            'name': '数据模拟',
            'icon': 'fas fa-flask',
            'url': '/chart/fake/'
        },
        {
            'name': '操作记录',
            'icon': 'fas fa-history',
            'url': '/admin/admin/logentry/'
        },
    ]
}
